from jili.tool.state import iswindows
from jili.data import load_localdata,save_localdata
import requests
class pcinfo0:
    def __init__(self):
        """
        Args:
            holddays: 0实时的，
            holddays:本地数据有效天数：注意打包成执行文件则使用实时有效既可
        """
        self.isconfig=True
        self.pcdata = {}
    def config(self,holddays):
        self.isconfig = False
        self.holddays=holddays
        if self.holddays!=0:
            pcdata = load_localdata("pcinfo",hold_days=self.holddays)
            if pcdata:
                self.pcdata=pcdata
            else:
                self.get_pcinfo()
                self.get_hhpart()
                self.get_pcname()
                self.get_mac()
                self.get_ip()
                self.get_local_ip()
                self.get_boardid()
                self.get_cpuid()
                self.get_hhid()
                save_localdata("pcinfo",self.pcdata)
    def check_config(self):
        if self.isconfig:
            self.config(None)
    def get_boardid(self):
        self.check_config()
        if "board_id" in self.pcdata.keys():
            rst=self.pcdata["board_id"]
        else:
            if iswindows:
                import wmi
                c = wmi.WMI()
                rst = c.Win32_BaseBoard()[0].SerialNumber.strip()
                self.pcdata["board_id"] = rst
            else:
                rst=""
        return rst
    def get_cpuid(self):
        self.check_config()
        if "cpuid" in self.pcdata.keys():
            rst = self.pcdata["cpuid"]
        else:
            if iswindows:
                import wmi
                c = wmi.WMI()
                rst = c.Win32_Processor()[0].ProcessorId.strip()
                self.pcdata["cpuid"] = rst
            else:
                rst = ""
        return rst
    def get_hhid(self):
        self.check_config()
        if "hhid" in self.pcdata.keys():
            rst = self.pcdata["hhid"]
        else:
            if iswindows:
                import wmi
                c = wmi.WMI()
                rst = c.Win32_DiskDrive()[0].SerialNumber.strip()
                self.pcdata["hhid"] = rst
            else:
                rst = ""
        return rst

    def get_local_ip(self):
        self.check_config()
        if "local_ip" in self.pcdata.keys():
            rst = self.pcdata["local_ip"]
        else:
            import socket
            # 获取本机电脑名
            myname = socket.getfqdn(socket.gethostname())
            # 获取本机ip
            rst = socket.gethostbyname(myname)
            self.pcdata["local_ip"] = rst
        return rst

    def get_ip(self):
        self.check_config()
        if "ip" in self.pcdata.keys():
            rst = self.pcdata["ip"]
        else:
            try:
                # 向提供公网 IP 查询服务的网站发送请求
                response = requests.get('https://ipinfo.io/json')
                # 检查响应状态码
                response.raise_for_status()
                # 解析 JSON 数据
                data = response.json()
                return data['ip']
            except requests.RequestException as e:
                print(f"请求出错: {e}")
            except KeyError:
                print("无法从响应中获取 IP 地址。")
            return None

        return rst

    def get_mac(self):
        self.check_config()
        if "mac" in self.pcdata.keys():
            rst = self.pcdata["mac"]
        else:
            import uuid
            mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
            mac = ":".join([mac[e:e + 2] for e in range(0, 11, 2)])
            rst = mac.upper()
            self.pcdata["mac"] = rst
        return rst

    def get_pcname(self):
        self.check_config()
        if "pcname" in self.pcdata.keys():
            rst = self.pcdata["pcname"]
        else:
            import socket
            rst = socket.gethostname()
            self.pcdata["pcname"] = rst
        return rst

    def get_hhpart(self):
        self.check_config()
        if "hhpart" in self.pcdata.keys():
            rst = self.pcdata["hhpart"]
        else:
            import psutil
            i = psutil.disk_partitions()[0]
            rst = i.mountpoint + "," + i.fstype + "," + i.opts
            self.pcdata["hhpart"] = rst
        return rst

    def get_pcinfo(self):
        self.check_config()
        if "pcinfo" in self.pcdata.keys():
            rst = self.pcdata["pcinfo"]
        else:
            rst =getlocal_ip()+"|"+get_ip()+"|"+get_mac()+"|"+get_hhid()+"|"+get_pcname()+"|"+get_cpuid()+"|"+get_hhpart()
            self.pcdata["pcinfo"] = rst
        return rst

#old function
pcinfo=pcinfo0()
getpcinfo=pcinfo.get_pcinfo
get_hhpart=pcinfo.get_hhpart
get_pcname=pcinfo.get_pcname
get_mac=pcinfo.get_mac
get_ip=pcinfo.get_ip
getlocal_ip=pcinfo.get_local_ip
getboard_id=pcinfo.get_boardid
get_cpuid=pcinfo.get_cpuid
get_hhid=pcinfo.get_hhid
if __name__ == "__main__":
    # print(getpcinfo())
    print(get_ip())